package com.example.abby.zyfypt331jj.model;


import com.example.abby.zyfypt331jj.iface.UserfocusListener;
import com.example.abby.zyfypt331jj.iface.Userfocusiface;
import com.example.abby.zyfypt331jj.service.UserfocusService;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.scalars.ScalarsConverterFactory;

/**
 * Created by Mac on 2017/6/3.
 */

public class UserfocusModel implements Userfocusiface {
    private Retrofit retrofit;
    private String BASEURL
            ="http://amicool.neusoft.edu.cn/";
    //构造函数
    public UserfocusModel()
    {   //使用Retrofit----1
        retrofit=new Retrofit.Builder()
                .baseUrl(BASEURL)
                .addConverterFactory(ScalarsConverterFactory.create())
                .build();
    }

    @Override
    public void focus(String mod, int id, String sessionID, final UserfocusListener Listener) {
        //使用Retrofit----2
        UserfocusService userfocusService=retrofit.create(UserfocusService.class);
        Call<String> call=userfocusService.focus(mod,id,sessionID);
        //使用Retrofit----3
        call.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                if(response.isSuccessful() && response!=null)
                {  if(response.body().trim().toString().equals("0"))
                    Listener.onResponse("1");
                else if(!response.body().trim().toString().contains("error"))
                    Listener.onResponse("2");
                else
                    Listener.onResponse("收藏："+response.body());
                }
                else   Listener.onFail("focus onresponse fail");
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                Listener.onFail("关注失败："+t.toString());
            }
        });
    }

    @Override
    public void unfocus(String mod, int id, String sessionID, final UserfocusListener Listener) {
        //使用Retrofit----2
        UserfocusService  userfocusService=retrofit.create(UserfocusService.class);
        Call<String> call=userfocusService.unfocus(mod,id,sessionID);
        //使用Retrofit----3
        call.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                if(response.isSuccessful() && response!=null)
                {  if(response.body().trim().toString().equals("0"))
                    Listener.onResponse("4");
                else if (response.body().trim().toString().equals("1"))
                    Listener.onResponse("5");
                else
                    Listener.onResponse("取消关注："+response.body());

                }
                else   Listener.onFail("unfocus onresponse fail");
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                Listener.onFail("取消关注失败："+t.toString());
            }
        });
    }

    @Override
    public void exist(String mod, int id, String sessionID, final UserfocusListener Listener) {
//使用Retrofit----2
        final UserfocusService  userfocusService=retrofit.create(UserfocusService.class);
        Call<String> call=userfocusService.exists(mod,id,sessionID);
        //使用Retrofit----3
        call.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                if(response.isSuccessful() && response!=null)
                {  if(response.body().trim().toString().equals("0"))//已收藏
                    Listener.onResponse("7");
                else if (response.body().trim().toString().equals("1"))//未收藏
                    Listener.onResponse("8");
                else
                    Listener.onResponse("判断关注："+response.body());

                }
                else   Listener.onFail("focus exist onresponse fail");//
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                Listener.onFail("判断关注失败："+t.toString());
            }
        });
    }
}
